<?php

namespace app\api\service;


use app\api\model\AiCompletions;
use app\api\model\AiCompletionsDetail;

class Share
{
    public function get($id)
    {
        $data = (new AiCompletionsDetail)
            ->where('completion_id', $id)
            ->order('id', 'desc')
            ->select();
        $chatId = 0;
        foreach ($data as &$val) {
            $val = $val->toArray();
            if (!$chatId) {
                $chatId = $val['chat_id'];
            }
            if ($val['content_type'] == 'json') {
                $content        = json_decode($val['content'], true);
                $val['content'] = '';
                $val['img']     = [];
                foreach ($content as $v) {
                    if ($v['type'] == 'text') {
                        $val['content'] .= $v['text'];
                    } else {
                        $val['img'][] = $v['image_url']['url'];
                    }
                }
            }
        }
        $type = (new AiCompletions)->where('id', $chatId)->value('type');

        return ['list' => $data, 'type' => $type];
    }
}
